<template>
  <el-dialog
      title="添加角色"
      v-model="dialogVisible"
  >
    <el-form
        :rules="addRoleRules"
        :model="editObj">
      <el-form-item label="角色名称"
                    prop="roleName">
        <el-input
            v-model="editObj.roleName"
            autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="roleDesc">
        <el-input
            v-model="editObj.roleDesc"
            autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
    <span class="dialog-footer">
      <el-button @click="show()">取 消</el-button>
      <el-button type="primary" @click="addRole">确 定</el-button>
    </span>
    </template>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "EditRoleBtn",
  //定义抛出的事件名称
  emits:['showEditDialog'],
  props: {
    editObj: Object,
    dialogVisible: Boolean
  },
  data() {
    return {
      addRoleRules: {
        roleName: [
          {required: true, message: '请输入角色名', trigger: 'blur'},
          {min: 2, max: 16, message: '请输入2-16个字符', trigger: 'blur'}
        ],
        roleDesc: [
          {required: true, message: '请输入角色描述', trigger: 'blur'},
          {min: 2, max: 16, message: '请输入2-16个字符', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    async addRole() {


      // this.$message.info("addRole click")
      const {data: res} = await axios.put('roles/' + this.editObj.roleId, {
        roleName: this.editObj.roleName,
        roleDesc: this.editObj.roleDesc,
      })

      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.$message.success(res.meta.msg)

      this.show()
    },
    show() {
      this.$emit('showEditDialog', false)
    },


  }, created() {
    // console.log("edit role created")
  }
}
</script>

<style scoped>

</style>